package com.dhcc.DRGsHosp.application.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.WardApplication;
import com.dhcc.DRGsHosp.core.domain.Ward;

@Named
@Transactional
public class WardApplicationImpl implements WardApplication {
	private static final Logger LOGGER = LoggerFactory.getLogger(WardApplicationImpl.class);
	
	private EntityRepository repository ;
	 @Autowired  
	 private NamedParameterJdbcTemplate jdbcTempSeverice; 
	 
	@Autowired
	private JdbcTemplateObj jdbcTemplateObj;
	private EntityRepository getEntityRepository() {
		if(repository == null ){
			repository = InstanceFactory.getInstance(EntityRepository.class, "repository" ) ;
		}
		return repository;
	}
	
	@Override
	public Ward getWard(String wardCode) {
		return Ward.get(Ward.class, wardCode);
	}
	@Override
	public void creatWard(Ward ward) {
		ward.save();
	}

	@Override
	public void updateWard(Ward ward) {
//		ward.save();
		if( null == ward ) return ;
		String depCode = ward.getDepCode() !=null ? ward.getDepCode() : "" ;
		String depGroupCode = ward.getDepGroupCode() != null ? ward.getDepGroupCode() : "" ;
		String wardDesc = ward.getWardDesc() != null ? ward.getWardDesc() : "null";
		String wardResume = ward.getWardResume() != null ? ward.getWardResume() : "null";
		String wardCode = ward.getWardCode() != null ? ward.getWardCode() : "";
		int wardOrder = ward.getWardOrder() != null ? ward.getWardOrder() : 0;
		String sql = "update drgs.t_ward ward set ward.ward_order = '"+wardOrder+"', \n" +
					"  ward.ward_desc = '"+wardDesc+"',ward.pardep_code = '"+depCode+"' ,ward.ward_resume = '"+wardResume+"' , ward.pardepgroup_code = '"+depGroupCode+"' \n" +
					" where  ward.ward_code = '"+wardCode+"' \n " ;
		getEntityRepository().createSqlQuery(sql).executeUpdate() ;
	}

	@Override
	public void removeWard(Ward ward) {
		if(ward != null){
			String wardCode = ward.getWardCode() != null ? ward.getWardCode() : "";
			String sql = "delete from drgs.t_ward ward where ward.ward_code = '"+wardCode+"' " ;
			getEntityRepository().createSqlQuery(sql).executeUpdate() ;
		}
	}

	@Override
	public void removeWards(Set<Ward> wards) {
		for (Ward ward : wards) {
			removeWard(ward) ;
		}
	}

	@Override
	public List<Ward> findAllWard() {
		return Ward.findAll(Ward.class);
	}

	@Override
	public List<Object[]> excuteQuery(String sql ,Map<String, Object> parameters) {
		// TODO Auto-generated method stub
		return getEntityRepository().createSqlQuery(sql).setParameters(parameters).list() ;
	}

	@Override
	public List<Map<String, Object>> findWardDescWithDep() {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer() ;
		sql.append(" select ward.ward_desc||'（'||dep.dep_name||'）' ward,ward.ward_code from DRGs.T_WARD ward  \n ") ;
		sql.append(" left join drgs.t_department dep on ward.pardep_code = dep.dep_code \n ") ;
		Map<String,Object> condition = new HashMap<>() ;
		List<Map<String, Object>> mapList = jdbcTempSeverice.queryForList(sql.toString(), condition) ;
		return mapList;
	}

	@Override
	public Map<String, Object> getDepAndDepGroupByWardCode(String wardCode) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer() ;
		sql.append(" select dep.dep_code,dep.dep_name,depgroup.depgroup_code,depgroup.depgroup_desc from drgs.t_ward ward  \n ") ;
		sql.append(" left join drgs.t_department dep on dep.dep_code = ward.pardep_code \n ") ;
		sql.append(" left join drgs.t_depgroup depgroup on depgroup.depgroup_code = ward.pardepgroup_code \n ") ;
		sql.append(" where ward.ward_code = :wardCode \n ") ;
		Map<String,Object> paramMap = new HashMap<>() ;
		paramMap.put("wardCode",wardCode) ;
		Map<String,Object> wardMap = jdbcTempSeverice.queryForMap(sql.toString(), paramMap) ;
		return wardMap;
	}
	/**
	 * 
	 *  @Description：监测顺序号是否存在
	 *  @Author lhj 
	 *  @see com.dhcc.DRGsHosp.application.WardApplication#isWardOrderExist(java.lang.String)
	 */
		@Override
		public List<Map<String, Object>> isWardOrderExist(String wardOrder) {
			String sql="select ward_code from drgs.t_ward where ward_order='"+wardOrder+"' \n";
			return jdbcTemplateObj.queryForList(sql);
		}
	
	/**  
	 *  @Description：
	 *  @Author mazhaoyan  
	 *  @see com.dhcc.DRGsHosp.application.WardApplication#updateWardNameByCode(com.dhcc.DRGsHosp.core.domain.Ward)  
	 */
	@Override
	public int updateWardNameByCode(Ward ward) {
		String sql = "update drgs.t_ward ward  set  ward.ward_desc = :wardDesc where ward.ward_code = :wardCode" ;
		SqlParameterSource paramSource = new BeanPropertySqlParameterSource(ward);
		return jdbcTempSeverice.update(sql, paramSource ) ;
	}
	
	/**  
	 *  @Description：
	 *  @Author mazhaoyan  
	 *  @see com.dhcc.DRGsHosp.application.WardApplication#updateWardCodeInIpinfo(java.lang.String, java.lang.String)  
	 */
	@Override
	public void updateWardCodeInIpinfo(String oldCode, String newCode) {
		String sql =  " update drgs.mr_ipinfo ip set ip.P00310001 = ? where ip.P00310001 = ? \n " ;
		try {
			jdbcTemplateObj.update(sql,new Object[] {newCode,oldCode}) ;
		} catch (Exception e) {
			LOGGER.error(e.getMessage());
		}
	}
	
	/**  
	 *  @Description：
	 *  @Author mazhaoyan  
	 *  @see com.dhcc.DRGsHosp.application.WardApplication#updateWardCodeInWard(java.lang.String, java.lang.String)  
	 */
	@Override
	public void updateWardCodeInWard(String oldCode, String newCode) {
		String sql = "update drgs.t_ward ward set ward.WARD_CODE = ? where ward.WARD_CODE = ? " ;
		try {
			jdbcTemplateObj.update(sql,new Object[] {newCode,oldCode}) ;
		} catch (Exception e) {
			LOGGER.error(e.getMessage());
		}
	}
	
	@Override
	public List<String> getWardNotification() {
		StringBuilder jpql = new StringBuilder("SELECT WARD_DESC FROM DRGS.T_WARD W  WHERE  W.PARDEP_CODE IS NULL");		
		List<String> sqlList = getEntityRepository().createSqlQuery(jpql.toString()).list();
		return sqlList;
	}

	@Override
	public int findMaxWardOrder() {
		String sql = " select max(t.ward_order)  from drgs.T_WARD t " ;
		Integer count = jdbcTemplateObj.queryForObject(sql, Integer.class) ;
		if( null == count){
			return 0 ;
		} else {
			return count;
		}
	}
}
